import process from './mockFlag.js';

// data.formatDD("yyyy-MM-DD hh:mm:ss");
Date.prototype.formatDD = function(formatStr){
	var date = this;
	var str = formatStr;
	str=str.replace(/yyyy|YYYY/,date.getFullYear());
	str=str.replace(/yy|YY/,(date.getYear() % 100)>9?(date.getYear() % 100).toString():"0" + (date.getYear() % 100));
	str=str.replace(/MM/,date.getMonth()>8?(date.getMonth()+1).toString():"0" + (date.getMonth()+1));
	str=str.replace(/M/g,date.getMonth()+1);
	str=str.replace(/dd|DD/,date.getDate()>9?date.getDate().toString():"0" + date.getDate());
	str=str.replace(/d|D/g,date.getDate());
	str=str.replace(/hh|HH/,date.getHours()>9?date.getHours().toString():"0" + date.getHours());
	str=str.replace(/h|H/g,date.getHours());
	str=str.replace(/mm/,date.getMinutes()>9?date.getMinutes().toString():"0" + date.getMinutes());
	str=str.replace(/m/g,date.getMinutes());
	str=str.replace(/ss|SS/,date.getSeconds()>9?date.getSeconds().toString():"0" + date.getSeconds());
	str=str.replace(/s|S/g,date.getSeconds());
	return str;
}

export default {
	utilName: {
		"tit": "公用utilJs中的内容zw，控制台还将会打印一些东西"
	},
	utilAlert () {
		console.log("公用util中的alert")
	},
	dom (id) {
		return document.getElementById(id);
	},
	domClass (searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null )
			node = document;
		if ( tag == null )
			tag = '*';
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (var i = 0, j = 0; i < elsLen; i++) {
			if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	},
	hasClass: function(ele, cls) {
	  cls = cls || "";
	  if (cls.replace(/\s/g, "").length == 0) return false; //当cls没有参数时,返回false
	  return new RegExp(" " + cls + " ").test(" " + ele.className + " ");
	},
	addClass: function(ele, cls) {
	  if (!this.hasClass(ele, cls)) {
	    ele.className = ele.className == "" ? cls : ele.className + " " + cls;
	  }
	},
	removeClass: function(ele, cls) {
	  if (this.hasClass(ele, cls)) {
	    var newClass = " " + ele.className.replace(/[\t\r\n]/g, "") + " ";
	    while (newClass.indexOf(" " + cls + " ") >= 0) {
	      newClass = newClass.replace(" " + cls + " ", " ");
	    }
	    ele.className = newClass.replace(/^\s+|\s+$/g, "");
	  }
	},
	/**
	 * @param {HTMLElement} element
	 * @param {string} className
	 */
	toggleClass(element, className) {
	  if (!element || !className) {
	    return
	  }
	  let classString = element.className
	  const nameIndex = classString.indexOf(className)
	  if (nameIndex === -1) {
	    classString += '' + className
	  } else {
	    classString =
	      classString.substr(0, nameIndex) +
	      classString.substr(nameIndex + className.length)
	  }
	  element.className = classString
	},
	scrollToTop () {
		const c = document.documentElement.scrollTop || document.body.scrollTop;
		if (c > 0) {
		  window.requestAnimationFrame(scrollToTop);
		  window.scrollTo(0, c - c / 8);
		}
	},
	/** 返回当前页的滚动位置。**/
	getScrollPosition: (el = window) => ({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
		y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop}),
	deepClone: function(origin, target) {
		if(target){
			// this.zutil.deepClone(b, a); -- 把a合并到b
			// 两个对象的合并
		  var target = target || {};
		  for (let prop in target) {
		    if (target.hasOwnProperty(prop)) {
		      if (target[prop] !== null && typeof target[prop] === 'object') {
		        origin[prop] = Object.prototype.toString.call(target[prop]) === '[object Array]' ? [] : {};
		        this.deepClone(origin[prop], target[prop]);
		      } else {
		        origin[prop] = target[prop];
		      }
		    }
		  }
		}else{
			// let c = this.zutil.deepClone(b); -- 把b复制给c
			// 对象的深拷贝
			if (typeof origin !== "object") return origin;
		  let newData = origin.constructor == Array ? [] : {};
		  const glob = typeof window === "undefined" ? global : window;
		  if (glob.JSON) {
		    /*使用JSON的方式实现拷贝*/
		    newData = JSON.parse(JSON.stringify(origin));
		  } else {
		    /*递归实现深拷贝*/
		    for (let i in origin) {
		      newData[i] = typeof origin[i] == 'object' ? this.deepClone(origin[i]) : origin[i];
		    }
		  }
		  return newData;
		}
	},
	zuiaccAdd: function(arg1,arg2){
		var r1,r2,m;
		try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
		try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
		m=Math.pow(10,Math.max(r1,r2))
		return (arg1*m+arg2*m)/m;
	},
	zuisubtr: function(arg1,arg2){
		var r1,r2,m,n;
		try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
		try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
		m=Math.pow(10,Math.max(r1,r2));
		n=(r1>=r2)?r1:r2;
		return ((arg1*m-arg2*m)/m).toFixed(n);
	},
	zuiaccMul: function(arg1,arg2) {
		var m=0,s1=arg1.toString(),s2=arg2.toString();
		try{m+=s1.split(".")[1].length}catch(e){}
		try{m+=s2.split(".")[1].length}catch(e){}
		return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
	},
	zuiaccDiv: function(arg1,arg2){
		var t1=0,t2=0,r1,r2;
		try{t1=arg1.toString().split(".")[1].length}catch(e){}
		try{t2=arg2.toString().split(".")[1].length}catch(e){}
		r1=Number(arg1.toString().replace(".",""));
		r2=Number(arg2.toString().replace(".",""));
		return (r1/r2)*Math.pow(10,t2-t1);
	},
	removeByIndex : function(arrays , dx){
		if(isNaN(dx)||dx>arrays.length){return false;}
		for(var i=0,n=0;i<arrays.length;i++){
			if(arrays[i]!=arrays[dx]){
				arrays[n++]=arrays[i]
			}
		}
		arrays.length-=1
		return arrays;
	},
	removeByValue : function(arrays, item ){
		for( var i = 0 ; i < arrays.length ; i++ ){
			if( item == arrays[i] ){
				break;
			}
		}
		if( i == arrays.length ){
			return;
		}
		for( var j = i ; j < arrays.length - 1 ; j++ ){
			arrays[ j ] = arrays[ j + 1 ];
		}
		arrays.length--;
		return arrays;
	},
	arrayLast: function(arrays){
		return arrays[arrays.length - 1];
	},
	getRandomArray: function(arr, count) {
		// 从一个数组中，随机取出count个元素组成新数组
	  var shuffled = arr.slice(0),
	    i = arr.length,
	    min = i - count,
	    temp, index;
	  while (i-- > min) {
	    index = Math.floor((i + 1) * Math.random());
	    temp = shuffled[index];
	    shuffled[index] = shuffled[i];
	    shuffled[i] = temp;
	  }
	  return shuffled.slice(min);
	},
	getCurDate(cDate, type){
		if(cDate && !(cDate instanceof Date))
			cDate = new Date(cDate);
		if(type && type == '2'){
			cDate = cDate.formatDD("yyyy-MM-DD hh:mm:ss");
		}else if(type){
			cDate = cDate.formatDD("yyyy-MM-DD hh:mm");
		}else{
			cDate = cDate.formatDD("yyyy-MM-DD");
		}
		return cDate;
	},
	lStorage: {
		setParam: function(name, value) {
			localStorage.setItem(name, value);
		},
		getParam: function(name) {
			return localStorage.getItem(name);
		},
		removeParam:function(name){
			localStorage.removeItem(name);
		},
		clearParam:function(){
			//清除所有的存储，谨慎使用
			localStorage.clear();
		}
	},
	closest: function(el, selector) {
		/**
		 * closest() 从元素本身开始，逐级向上级元素匹配，并返回最先匹配的祖先元素（包括自己本身）
		 * 它同parents()方法类似，不同点在于它只返回匹配的第一个元素节点。
		 * <div class="secwrap" @click="chooseSec($event, scope.row)">
        <el-button class="zbt secBtn" :class="{act: scope.row.level==index}" v-for="(item, index) in arr1" :key="index">{{item}}次</el-button>
      </div>
		 * chooseSec (ev) {
	      let e = ev || window.event;
	      let target = e.target || e.srcElement;
	      let _zparent = this.zutil.closest(target, '.secwrap');
	      let _zbtns = _zparent.querySelectorAll('.secBtn');
	    },
		**/
	  var matchesSelector = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
	  while (el) {
	    if (matchesSelector.call(el, selector)) {
	      break;
	    }
	    el = el.parentElement;
	  }
	  return el;
	},
	createUniqueString() {
	  const timestamp = +new Date() + ''
	  const randomNumS = parseInt((1 + Math.random()) * 65536) + ''
	  return (+(randomNumS + timestamp)).toString(32)
	},
	randomNum: function (minNum, maxNum) {
		//生成从minNum到maxNum的随机数
		switch (arguments.length) {
			case 1:
				return parseInt(Math.random() * minNum + 1, 10);
				break;
			case 2:
				return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
				break;
			default:
				return 0;
				break;
		}
	},
	formatAxiosData(data) {
	  if (!process.env.MOCK) {
	  	return data.data;
	  }else{
			return data;
			/* let _num = this.randomNum(500, 1000);
			return new Promise(function(resolve, reject) {
				setTimeout(()=>{
					resolve(data);
				}, _num)
			}) */
	  }
	},
	hasOwn(obj, key) {
		return Object.prototype.hasOwnProperty.call(obj, key);
	},
	elementIsVisibleInViewport: (el, partiallyVisible = false) => {
		/**
		 * 如果指定的元素在视区中可见, 则返回true, 否则为false。
		 * 第二个参数指定true时允许只见部分，否则默认见到全部。
		 * 本方法只适用于元素高度不超过整屏高度的情况。
		 * elementIsVisibleInViewport(document.getElementById("test"))
		**/
	  const { top, left, bottom, right } = el.getBoundingClientRect();
	  return partiallyVisible ?
	    ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
	    ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth)) :
	    top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
	},
	getURLParameters: (url = window.location.href) => {
		// getURLParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
		url.match(/([^?=&]+)(=([^&]*))/g).reduce((a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {})
	},
	ztype: {},
	_isType: function(type) {
	  if (!this.ztype[type]) {
	    this.ztype[type] = function(obj) {
	      return Object.prototype.toString.call(obj) === '[object ' + type + ']';
	    };
	  }
	  return this.ztype[type];
	},
	isArray: function(obj) {
	  return this._isType('Array')(obj);
	},
	isString: function(obj) {
	  return this._isType('String')(obj);
	},
	isBoolean: function(obj) {
	  return this._isType('Boolean')(obj);
	},
	isFunction: function(obj) {
	  return this._isType('Function')(obj);
	},
	isDate: function(obj) {
	  return this._isType('Date')(obj);
	},
	isNumber: function(obj) {
	  return this._isType('Number')(obj);
	},
	isNaN: function(obj) {
	  return obj !== obj;
	},
	isNull: function(obj) {
	  return obj === null;
	},
	isUndefined: function(obj) {
	  return obj === void 0;
	},
	isNumeric: function(obj) {
	  return obj - parseFloat(obj) >= 0;
	},
	isPlainObject: function(obj) {
	  return obj && typeof obj === "object" && Object.getPrototypeOf(obj) === Object.prototype;
	},
	isEmpty: function(obj) {
	  return this.isNull(obj) || this.isUndefined(obj) || this.isString(obj) && obj === "";
	},
	separator: function(num) {
		/*给数字增加千位分隔符*/
	  var res = null;
	  if (this.isNaN(Number(num))) {
	    res = '非数字';
	  } else {
	    res = num.toString().replace(/\d+/, function(n) {
	      return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) {
	        return $1 + ",";
	      });
	    })
	  }
	  return res;
	},
	trim: function(str) {
		// 去除字符串两边的空格
	  if (!this.isString(str)) {
	    str = String(str);
	  }
	  if (this.isFunction(String.prototype.trim)) {
	    return String.prototype.trim.call(str);
	  } else {
	    return str.replace(/(^\s*)|(\s*$)/g, '');
	  }
	},
	clearNoNum(obj, type) {
		// <input class="weui-input J_COD" type="text" placeholder="请输入PAM(kg)" onkeyup='clearNoNum(this)' onblur='clearNoNum(this)'>
	  if (type) {
	    obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符  
	    obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的  
	    obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	    if (type == '1') {
	      obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3'); //只能输入一位小数
	    } else if (type == '2') {
	      obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d{2}).*$/, '$1$2.$3'); //只能输入两个小数
	    } else if (type == '3') {
	      obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d{3}).*$/, '$1$2.$3'); //只能输入三位小数
	    }
	    //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额
	    if (obj.value.indexOf(".") < 0 && obj.value != "") {
	      obj.value = parseFloat(obj.value);
	    }
	    //控制.开头的数字，如：.8798321
	    // \.16513000.546
	    if (obj.value && !obj.value.split(".")[0]) {
	      obj.value = '0.' + (obj.value.split(".")[1] || '');
	      clearNoNum(obj);
	    }
	    if (!obj.value)
	      obj.value = 0;
	  } else {
	    // 限制输入正整数
	    obj.value = obj.value.replace(/[^\d]/g, "");
	  }
	},
	getDomStyle () {
		// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
		// let styL = getDomStyle(dragDom, 'left');
		if (window.document.currentStyle) {
			return (dom, attr) => dom.currentStyle[attr];
		} else{
			return (dom, attr) => getComputedStyle(dom, false)[attr];
		}
	},
	// uniapp 扩展
	zconfirm (msg, callBackFun) {
		uni.showModal({
			title: '医内物流',
			content: msg,
			success: function (res) {
				if (res.confirm) {
					callBackFun(true)
				} else if (res.cancel) {
					callBackFun(false)
				}
			}
		})
	},
	zalert (msg, callBackFun) {
		uni.showModal({
			title: '医内物流',
			content: msg,
			showCancel: false,
			success: function () {
				if (!!callBackFun) {
					callBackFun()
				}
			}
		});
	},
	ztip (msg) {
		uni.showToast({
			title: msg,
			duration: 2000,
			mask: false,
			icon: "none",
		})
	},
	zuiShaker () {
		// 振动
		if(uni.getSystemInfoSync().platform == 'ios'){
			uni.vibrateShort({
				success: function () {}
			});
		}else{
			uni.vibrateLong({
				success: function () {}
			});
		}
	}
}